<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>天气查询</title>
    <script src="/jquery.min.js"></script>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            margin: 20px;
            background-color: #f8f8f8;
            color: #333;
        }

        h1, h2 {
            color: #4CAF50;
        }

        input {
            padding: 10px;
            margin-bottom: 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }

        th, td {
            padding: 12px;
            text-align: center;
            border: 1px solid #ddd;
        }

        th {
            background-color: #4CAF50;
            color: white;
        }

        td {
            background-color: #f9f9f9;
        }

        span {
            font-weight: bold;
            color: #333;
        }
    </style>
</head>
<body>
<input type="text" id="citys">
<input type="button" value="查询" onclick="selectWeather()">
<h1>天气信息：<span id="city"></span></h1>
<h2>当前天气</h2>
<p>
    温度：<span id="temperature"></span>°C<br>
    湿度：<span id="humidity"></span>%<br>
    天气状况：<span id="info"></span><br>
    风向：<span id="direct"></span>，风力：<span id="power"></span>
</p>
<h2>未来天气预报</h2>
<table border="1" id="weatherTable">
    <tr>
        <th>日期</th>
        <th>温度（°C）</th>
        <th>天气状况</th>
        <th>风向</th>
    </tr>
</table>
<script>
    function selectWeather() {
        $.ajax({
            url: "/weather/selectWeather",
            type: "get",
            data: {city: $("#citys").val()},
            dataType: "json",
            success: function (data) {
                console.log(data);
                // 清空表格内容
                $('#weatherTable').empty();
                $("#city").html(data.result.city)
                $("#temperature").html(data.result.realtime.temperature)
                $("#humidity").html(data.result.realtime.humidity)
                $("#info").html(data.result.realtime.info)
                $("#direct").html(data.result.realtime.direct)
                $("#power").html(data.result.realtime.power)
                var day = data.result.future;
                var table = $('#weatherTable');
                // 重新添加表头
                table.append('<tr><th>日期</th><th>温度（°C）</th><th>天气状况</th><th>风向</th></tr>');
                // 添加表体内容
                $(day).each(function (i, v) {
                    var row = '<tr>' +
                        '<td>' + v.date + '</td>' +
                        '<td>' + v.temperature + '</td>' +
                        '<td>' + v.weather + '</td>' +
                        '<td>' + v.direct + '</td>' +
                        '</tr>';
                    table.append(row);
                });
            },
            error: function () {
                alert("查询失败");
            }
        });
    }
</script>
</body>
</html>
